package com.unkow.first.telegram.config;

import com.unkow.first.telegram.handler.TelegramMessageHandler;
import com.unkow.first.telegram.util.TelBotUtil;
import com.unkow.first.telegram.vo.TelBotConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

/**
 * 这是一个全局通用的兜底机器人，正常只会在common-service启动，业务系统有独有的机器人请使使用TelBotUtil
 * todo 启动多个服务里可能会报异常，这里暂时不考虑
 */
@Configuration
@ConditionalOnProperty(value = "system.telegram.bot.enabled", havingValue = "true")
@ComponentScan(basePackages = {"com.unkow.first.telegram"})
public class TelegramBotConfig {


    @Autowired(required = false)
    private TelegramMessageHandler messageHandler;

    @Bean(value = "telegramLongPollingBot")
    public TelegramLongPollingBot telegramLongPollingBot(@Qualifier("defaultTelBotConfigVO") TelBotConfigVO telBotConfigVO) throws TelegramApiException {
        return TelBotUtil.handler.initBot(telBotConfigVO, messageHandler);
    }

    @Value("${system.telegram.bot.config.USERNAME:}")
    private String botUsername;
    @Value("${system.telegram.bot.config.TOKEN:}")
    private String botToken;
    @Value("${system.telegram.bot.config.PROXY_HOST:}")
    private String proxyHost;
    @Value("${system.telegram.bot.config.PROXY_PORT:0}")
    private Integer proxyPort;

    @Bean
    public TelBotConfigVO defaultTelBotConfigVO() {
        return new TelBotConfigVO(botUsername, botToken, proxyHost, proxyPort);
    }

}
